package server

import (
	"fmt"
	"log"

	"com.zkh/agent/coap/conf"
	"com.zkh/agent/coap/global"
	"com.zkh/agent/coap/service"
	"com.zkh/agent/coap/tcp"
)

func CoapTcpServer() {
	address := fmt.Sprintf("0.0.0.0:%d", conf.GetCoapPort())
	server := tcp.New(address)
	server.AddRouter("receive", service.ForwardToDeviceCloud)

	// server.OnNewMessage(func(c *tcp.Client, req *pool.Message) {
	// 	path, _ := req.Path()
	// 	body, _ := req.ReadBody()
	// 	fmt.Printf("path:%s,body:%s\n", path, body)
	// })
	server.OnClientConnectionClosed(func(c *tcp.Client, err error) {
		// connection with client lost
		log.Printf("Client %s close!", c.GetId())
		server.Manager.Remove(c)
	})

	global.CoapServer = server
	server.Listen()
}
